//
//  HMHomeViewController.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/4/21.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit


private let cellId = "cellId"

class HMHomeViewController: HMBaseViewController {
    
     lazy var listViewModel = HMStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
         
    }
    
    override func loadData() {
       
        
      listViewModel.loadStatus(pullup: self.isPullUp) { (isSuccess,shouldReflesh) in
        
        print("加载数据完成")
        //结束刷新
        self.refleshControl?.endRefreshing()
        
        //恢复上拉刷新标记
        self.isPullUp = false
        
        
        if shouldReflesh{
             self.tableView?.reloadData()
        }

   }
        
        
        
  
}
    func showFriends()  {
        print(#function)
        let vc = HMDemoViewController()
        //FIXME：去掉底部tabbar
        //vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
}
//MARK:设置数据源方法
extension HMHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        return cell
    }
}
//MARK:设置界面
extension HMHomeViewController{
    //重写父类方法
    override func setupTableView() {
     super.setupTableView()
        //无法高亮 也就是没法实现我想要的效果 所以需要自定义试图 APP都是自定义试图

        navigaItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, action:#selector(showFriends) , target: self)
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupNavTitle()
    }
    //MARK:设置导航栏状态 昵称和头像
    private func setupNavTitle(){
        
        
       let button = HMTitleButton(title: HMShareManager.shared.userAccount.screen_name)
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        
        navigaItem.titleView = button
        
        
        
    }
    @objc private func clickTitleButton(btn:UIButton){
        
        btn.isSelected = !btn.isSelected
    }
}
